﻿/* 
 *  <copyright file="GameplayEventMapper.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using SobrietyEngine.PlayerData;
using SobrietyEngine.Input;
using SobrietyEngine.User;
using SobrietyEngine.Events;
using SobrietyEngine.Events.Input;
using SobrietyEngine.Guids;
using SobrietyEngine.Input.Mapping;
using SobrietyEngine.Events.UserActions;
using SobrietyEngine.Events.Physics;
using SobrietyEngine.Events.Screens;

using SEGameExample.Events.WorldMap;
using SEGameExample.Events.UserActions;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace SEGameExample.InputMappers
{
    public class PlatformerEventMapper:BaseMapper
    {
        List<Keys> KeysDown = new List<Keys>();
        Vector3 lastMoveBy = Vector3.Zero;
        Vector3 lastRotateBy = Vector3.Zero;

        public PlatformerEventMapper(IEventRouter eventRouter, CGuid owner):base(eventRouter, owner)
        {
        }

        public override void RegisterEventListeners()
        {

            EventRouter.AddEventListener(typeof(JoystickMove), this);
            EventRouter.AddEventListener(typeof(ButtonPress), this);
            EventRouter.AddEventListener(typeof(ButtonRelease), this);
            EventRouter.AddEventListener(typeof(Trigger), this);
            EventRouter.AddEventListener(typeof(KeyPress), this);
            EventRouter.AddEventListener(typeof(KeyRelease), this);
            Enabled = true;
        }

        public override void UnregisterEventListeners()
        {
            EventRouter.RemoveEventListener(this);
            Enabled = false;
        }

        //joystick / keyboard input events come in, gameServices events go out
        public override void HandleEvent(Event e)
        {
            if (!IsInputMine(e))
                return;

            if (e.Type == typeof(JoystickMove))
            {
                JoystickMove jm = (JoystickMove)e.Data;

                if (jm.Left == true) // left joystick
                    EventRouter.RaiseEvent(this, new Move(Owner, new Vector2(jm.X, jm.Y)), Owner);
                else
                    EventRouter.RaiseEvent(this, new Rotate(Owner, new Vector3(jm.X, jm.Y,0)), Owner);
                return;
            }
            else if (e.Type == typeof(ButtonPress))
            {
                ButtonPress bp = (ButtonPress)e.Data;

                if (bp.Button == GamepadEnums.GamepadButton.A)
                    EventRouter.RaiseEvent(this, new Jump(Owner), Owner);

                else if (bp.Button == GamepadEnums.GamepadButton.RightBumper)
                    EventRouter.RaiseEvent(this, new SuperJump(Owner), Owner);

                else if (bp.Button == GamepadEnums.GamepadButton.LeftBumper)
                    EventRouter.RaiseEvent(this, new ToggleDebugConsole(), Owner);

                else if (bp.Button == GamepadEnums.GamepadButton.X)
                    EventRouter.RaiseEvent(this, new Use(Owner), Owner);

                else if (bp.Button == GamepadEnums.GamepadButton.Y)
                    EventRouter.RaiseEvent(this, new TogglePhysicsDebug("crap, need scene fo this to work from here"));

                else if (bp.Button == GamepadEnums.GamepadButton.B)
                    EventRouter.RaiseEvent(this, new ExitLevel());

                return;
            }

            //keyboard stuff
            else if (e.Type == typeof(KeyPress))
            {
                KeysDown.Add(((KeyPress)e.Data).Key);
            }
            else if (e.Type == typeof(KeyRelease))
            {
                KeysDown.Remove(((KeyRelease)e.Data).Key);
            }



            //do actiosn based on keys down
            Vector3 moveBy = Vector3.Zero;
            Vector3 rotateBy = Vector3.Zero;
            foreach (Keys k in KeysDown)
            {
                if (k == Keys.Right)
                    moveBy.X += 1;

                else if (k == Keys.Left)
                    moveBy.X -= 1;

                else if (k == Keys.Up)
                    moveBy.Y += 1;

                else if (k == Keys.Down)
                    moveBy.Y -= 1;

                else if (k == Keys.PageUp)
                    moveBy.Z += 1;

                else if (k == Keys.PageDown)
                    moveBy.Z -= 1;

                else if (k == Keys.A)
                    EventRouter.RaiseEvent(this, new Jump(Owner), Owner);

                else if (k == Keys.S)
                    EventRouter.RaiseEvent(this, new SuperJump(Owner), Owner);

                else if (k == Keys.Space)
                    EventRouter.RaiseEvent(this, new Use(Owner), Owner);

                else if (k == Keys.Escape)
                    EventRouter.RaiseEvent(this, new ExitLevel());
            }

            if (moveBy != Vector3.Zero || lastMoveBy != Vector3.Zero)
            {
                EventRouter.RaiseEvent(this, new Move(Owner, moveBy), Owner);
                lastMoveBy = moveBy;
            }

            if (rotateBy != Vector3.Zero || lastRotateBy != Vector3.Zero)
            {
                EventRouter.RaiseEvent(this, new Rotate(Owner, rotateBy), Owner);
                lastRotateBy = rotateBy;
            }
        }
    }
}
